GBML manual / Using the editor

Building code with blocks

A friendly walk through making a script by dragging blocks - the panel, dropping a block in, If/compare, holes, the gray CODE block, notes, Blocks and Text side by side, and choosing your own block colors.

The short version

Blocks are a way to write code without typing it. Instead of spelling out if (hp <= 0), you drag an If/compare block into your script and fill in its holes. What you build is the same code you would have typed - the same script, just drawn as colored pieces you can pick up and move.

You never have to choose between the two. Every code window can show the same script as Blocks, as Text, or (in the docked editor) both next to each other. Change one and the other follows. If you want the short reference, it is on the Blocks and code page. This page is the walk-through: we will build something, one step at a time.

Open the blocks

Every code window - an object's event, a script, a room's creation code - has a thin bar above its code with Blocks and Text. Press Blocks, or press Ctrl+Shift+B with the window selected. Press it again to flip back to the text.

A script window in Blocks view, with the panel on the left and the blocks on the rightA script window in Blocks view, with the panel on the left and the blocks on the right
A script window in Blocks view, with the panel on the left and the blocks on the right

Two things sit side by side:

You can drag the edge of the panel to make it wider or thinner. The little arrow button beside the search box folds the whole panel into a thin strip of colored dots - click a dot and the panel opens on that section. Panel to the right swaps the sides. Gbabel remembers all of it.

Your first block

Let us make a script that adds to a score and plays a sound. Open a script, press Blocks, and:

  1. Open the Variables section and find change x by 1. Drag it onto the dotted area. Let go on empty space: it becomes a new piece of your script.
  2. Click the little x in the block and pick (or type) score. Click the 1 and type 10. The block now reads change score by 10.
  3. Open Sounds. Drag play sound so that a glowing line shows up under your first block, then let go.
A block being dragged in, with the glowing drop line between two blocksA block being dragged in, with the glowing drop line between two blocks
A block being dragged in, with the glowing drop line between two blocks

That glowing line is the whole trick. It shows you exactly where the block will land: between two blocks, inside another block, or in a hole. Let go, and it lands. If you change your mind while you are holding a block, press Escape and nothing happens.

Now press Text. You will see:

score += 10;
sound_play(snd_coin);

That is what you built (with your own sound's name where it says snd_coin, and no volume because you left that hole empty). Nothing is hidden and nothing extra was added.

Some holes are lists. A hole that wants the name of one of your own things - an object, a sprite, a sound, a room, a path - shows a dropdown of the ones in your project, so you cannot misspell one. A hole that wants a number or some words just takes typing.

To move a block, drag it. It takes everything inside it along. To put a block into a hole of another block (a calculation inside a hole, say), drop it right on the hole.

If/compare, and, or, else

If/compare is the block that decides things. It has a body, and what is in the body only runs when the comparison is true.

Read it left to right: a left side, an operator (=, <, <= and so on), and a right side. Say a hero has hp (health) and lives, and you want to destroy him only when he has no health and he has lives left to lose:

  1. Drag If/compare in. Set it to hp, <=, 0.
  2. Press + and/or. A second row appears. Set the little word to and and the row to lives, >, 0. (A row has an x if you want to take it out again.)
  3. Drag destroy instance into the body.

Want something to happen when that was not true? Press + else if to add another comparison with its own body - it is only looked at when the one above it was false. Press else to add a last body that runs when nothing above matched. Now the block has grown from a C shape into an E shape:

An If/compare block with and, else if and elseAn If/compare block with and, else if and else
An If/compare block with and, else if and else

In text, that is:

if (hp <= 0 && lives > 0) {
    instance_destroy(self);
    sound_play(snd_fall);
} else if (hp < 3) {
    x += 1;
} else {
    score += 1;
}

The rounded Compare block (the second one in the Logic section) is the same comparison with no body. It is for holes that want a true or false, like the condition of a loop.

Remember something: declare local variable

Sometimes you want to work something out once and use the answer a few times. Declare local variable gives it a name. It only exists inside the script you are in, and it is gone when the script ends.

A declare local variable block holding a calculationA declare local variable block holding a calculation
A declare local variable block holding a calculation

Type the name (spread) in the first box and the value in the second. The value can be a number, another name, or a whole little calculation like (random(8) - 4) * facing. Anything after that can use spread - a dropdown that wants a variable lists it, because Gbabel knows it is there.

Arrays and structs

Open the Data section and you will find two capsule-shaped blocks, array and struct. They are not steps on their own: they fit into a hole, because they are values.

The gray CODE block (nothing you type is ever lost)

Almost everything you can write is a block. But not quite everything, not yet. When Gbabel meets a line it has no block for, it does not guess and it does not throw it away. It shows the line as a gray CODE block, holding exactly what you wrote:

A gray CODE block between two ordinary blocksA gray CODE block between two ordinary blocks
A gray CODE block between two ordinary blocks

You can click inside it and edit the line right there. The blocks above and below stay ordinary blocks. So a script is never "too fancy for blocks": use blocks for the easy parts and type the rare ones.

If a script has a mistake Gbabel cannot read at all (a missing bracket, say), the whole script shows as one gray CODE block with a short note above it. Nothing was changed. Fix the mistake in Text, press Blocks, and the blocks are back. You can even drop new blocks below it in the meantime, and the text above stays exactly as it was.

Notes, and the right-click menu

A comment in your code - a line starting with // - shows as a yellow note block with a pencil on it. Notes are for you. They do nothing when the game runs. You can click a note and rewrite it, drag it, delete it, like any other block.

Right-click any block for a small menu:

The right-click menu on a blockThe right-click menu on a block
The right-click menu on a block

Every one of these is a single step of Undo. If it goes wrong, Ctrl+Z.

Blocks, Text, and both at once

In the docked editor there is a third button next to Blocks and Text: Side by side. It shows the blocks and the text together:

The blocks and the text side by side, with a clicked block's lines tintedThe blocks and the text side by side, with a clicked block's lines tinted
The blocks and the text side by side, with a clicked block's lines tinted

This is the best way to learn, because you can watch what each block is made of.

One more thing about Undo. Typing counts as one step for as long as you keep typing in the same window, and each thing you do with blocks is a step of its own. So Ctrl+Z takes back the last thing you did, whichever side you did it on.

Choosing your own colors

Each kind of block has its own color - Logic is gold, Movement is purple, Sounds are pink - so you can see what a script is made of at a glance. If you would like different colors, they are yours to change.

Open File, then Preferences, and look for Block colors:

The Block colors part of PreferencesThe Block colors part of Preferences
The Block colors part of Preferences

There is one row for every kind of block. Click the colored square to pick a color, or type an exact one (like #7a5cd6) in the box. The change shows straight away, in the blocks and in the panel behind the window. The little arrow at the end of a row puts that one kind back to its normal color, and Back to the normal colors puts all of them back.

The same blocks, with Logic changed to purple and Loops to pinkThe same blocks, with Logic changed to purple and Loops to pink
The same blocks, with Logic changed to purple and Loops to pink

Your colors are remembered on this computer, the same way the code editor's colors are. They are not part of your game: nobody who opens your project sees them, and they never change how a block works.

When something does not look right

See also: Blocks and codeThe Code tabTips and shortcutsWhat GBML is